package com.plm.k8s.myk8sapi.cmd.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.plm.k8s.myk8sapi.cmd.AbstractLocalCmd;
import com.plm.k8s.myk8sapi.cmd.IHpaCmd;
import com.plm.k8s.myk8sapi.pojo.entity.hpa.ApiHpaEntity;
import com.plm.k8s.myk8sapi.pojo.entity.hpa.HpaEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author 潘磊明
 * 版本号：1.0
 * 创建日期：2021/8/3 18:13
 */
@Service
public class HpaCmdImpl extends AbstractLocalCmd implements IHpaCmd {

    @Override
    public boolean createHpa(String name, String resourceKind, String resourceName,
            Integer min, Integer max, Integer cpuPercent, String namespace) throws Exception {
        if (!StringUtils.hasText(name)) throw new Exception("Hpa的名称不能为空");
        if (!StringUtils.hasText(resourceKind)) throw new Exception("监控的资源类型不能为空");
        if (!StringUtils.hasText(resourceName)) throw new Exception("监控的资源名称不能为空");
        if (max == null) throw new Exception("最大的副本数不能为空");
        if (min != null && max != null && min >= max) throw new Exception("最小副本数不能大于等于最大副本数");
        String cmd = generateCreateHpaCmd(name, resourceKind, resourceName, min, max, cpuPercent, namespace);
        String result = exec(cmd);
        if (!String.format("horizontalpodautoscaler.autoscaling/%s autoscaled", name).equals(trimBr(result))) {
            throw new Exception(result);
        }
        return true;
    }

    @Override
    public List<HpaEntity> listHpa(boolean isAll, String namespace, String nameFilter,
            Map<String, String> labelFilter) throws Exception {
        String cmd = generateListHpaCmd(isAll, namespace, nameFilter, labelFilter);
        ApiHpaEntity apiHpaEntity = execForYamlObject(cmd, ApiHpaEntity.class);
        if (apiHpaEntity == null) return Collections.emptyList();
        return apiHpaEntity.getItems();
    }

    @Override
    public boolean deleteHpa(String name, String namespace) throws Exception {
        if (!StringUtils.hasText(name)) throw new Exception("Hpa名称不能为空");
        String cmd = generateDeleteCmd(name, namespace);
        String result = exec(cmd);
        if (!String.format("horizontalpodautoscaler.autoscaling \"%s\" deleted", name).equals(trimBr(result))) {
            throw new Exception(result);
        }
        return true;
    }

    private String generateDeleteCmd(String name, String namespace) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("delete hpa ").append(name);
        if (StringUtils.hasText(namespace)) sb.append(" -n ").append(namespace);
        return sb.toString();
    }

    private String generateListHpaCmd(boolean isAll, String namespace, String nameFilter, Map<String, String> labelFilter) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("get hpa -o yaml");
        if (isAll) {
            sb.append(" -A");
        } else {
            if (StringUtils.hasText(namespace)) addNamespace(sb, namespace);
        }
        if (StringUtils.hasText(nameFilter)) addNameFilter(sb, nameFilter);
        if (CollectionUtil.isNotEmpty(labelFilter)) addLabelFilter(sb, labelFilter);
        return sb.toString();
    }


    private String generateCreateHpaCmd(String name, String resourceKind, String resourceName,
            Integer min, Integer max, Integer cpuPercent, String namespace) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("autoscale ").append(resourceKind).append(" ").append(resourceName);
        sb.append(" --name=").append(name);
        if (min != null) sb.append(" --min=").append(min);
        if (max != null) sb.append(" --max=").append(max);
        if (cpuPercent != null) sb.append(" --cpu-percent=").append(cpuPercent);
        if (StringUtils.hasText(namespace)) addNamespace(sb, namespace);
        return sb.toString();
    }
}
